/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import dbs.Broadcast;
import dbs.Comment;
import dbs.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
 * 
 */
@Stateless
@LocalBean
public class GetBean {
    
    @EJB
    private UserFacade userFacade;
    
    @EJB
    private BroadcastFacade broadcastFacade;
    
    @EJB
    private CommentFacade commentFacade;

    /**
     * get broadcasted messages in channelId.
     * 
     * @param channelId
     * @return message list
     */
    public List<Map<String, Object>> getMessages(long channelId)
    {
        List<Broadcast> msgs =  broadcastFacade.findBoradcastMessages(channelId);
        
        List<Map<String, Object>> out = new ArrayList<Map<String, Object>>();
        
        for(Broadcast broadcast : msgs)
        {
            Map<String, Object> msgData = new HashMap<String, Object>();
            
            msgData.put("content", broadcast.getContent());
            msgData.put("messageID", broadcast.getId());
            msgData.put("date", broadcast.getDate().getTime());
            
            out.add(msgData);
        }
        
        return out;
    }
    
    /**
     * get comments in channelId
     * 
     * @param channelId
     * @return 
     */
    public List<Map<String, Object>> getComment(long channelId)
    {
        List<Comment> comments =  commentFacade.findCommentList(channelId);
        
        List<Map<String, Object>> out = new ArrayList<Map<String, Object>>();
        
        for(Comment comment : comments)
        {
            Map<String, Object> comData = new HashMap<String, Object>();
            
            User commentUser =  userFacade.findUser(comment.getWriterID());
            
            comData.put("content", comment.getContent());
            comData.put("writerID", comment.getWriterID());
            comData.put("writerName", commentUser.getName());
            comData.put("channelID", channelId);
            comData.put("date", comment.getDate());
            
            out.add(comData);
        }
        
        return out;
    }
}
